﻿#include "PlatformProperty.h"
#include "UnitUtil.h"
using namespace ecarx::vhal::core::property::UnitUtil;

//3.2.2 [SW_FC_005_004]单位设置
SYSTEM_READ_WRITE_PROPERTY(HVAC_TEMPERATURE_DISPLAY_UNITS, int32_t) {
    builder.area(VehicleArea::GLOBAL)
        .initValue(
            []() {
                VHALOG.d(
                    "withValueBySignals HVAC_TEMPERATURE_DISPLAY_UNITS : cc161[%d] "
                    "usgmod[%d]",
                    SIGNAL.getcarconfig161(), SIGNAL.getVehModMngtGlbSafe1UsgModSts());
                if (SIGNAL.getcarconfig161() == sdb::CarConfig161(-1)) {
                    VHALOG.d("HVAC_TEMPERATURE_DISPLAY_UNITS getcarconfig161 failed ,default: CELSIUS");
                    return toInt(VehicleUnit::CELSIUS);
                }
                if (SIGNAL.carConfigIs(
                        SignalRX::carconfig161, sdb::CarConfig161::_01, sdb::CarConfig161::_02,
                        sdb::CarConfig161::_03, sdb::CarConfig161::_04, sdb::CarConfig161::_05,
                        sdb::CarConfig161::_06, sdb::CarConfig161::_07, sdb::CarConfig161::_08,
                        sdb::CarConfig161::_09, sdb::CarConfig161::_0A, sdb::CarConfig161::_0B,
                        sdb::CarConfig161::_0C, sdb::CarConfig161::_0D, sdb::CarConfig161::_0E,
                        sdb::CarConfig161::_0F, sdb::CarConfig161::_10)) {
                    VHALOG.d("HVAC_TEMPERATURE_DISPLAY_UNITS : CELSIUS ");
                    return toInt(VehicleUnit::CELSIUS);
                } else {
                    VHALOG.d("HVAC_TEMPERATURE_DISPLAY_UNITS : FAHRENHEIT ");
                    return toInt(VehicleUnit::FAHRENHEIT);
                }
            },
            PersistType::Profile)
        .withStatusBySignals(
            []([[maybe_unused]] const PropertyStatus& last) {
                VHALOG.d("withStatusBySignals HVAC_TEMPERATURE_DISPLAY_UNITS : last[%d]", last);
                if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                    return PropertyStatus::Active;
                }
                return PropertyStatus::NotActive;
            },
            SignalRX::VehModMngtGlbSafe1UsgModSts)
        .withValueBySignals(
            []([[maybe_unused]] const int32_t &last) {
                VHALOG.d("withValueBySignals HVAC_TEMPERATURE_DISPLAY_UNITS : last[%d]", last);
                return last;
            })
        .onSetProperty([this](const int32_t& v) {
            auto lastUnit = getValue(toInt(VehicleArea::GLOBAL));
            VHALOG.d("HalOnSetProperty HVAC_TEMPERATURE_DISPLAY_UNITS : [%d] lastUnit[%d]", v,
                  lastUnit);

            if (v == toInt(VehicleUnit::CELSIUS)) {
                SIGNAL.setTmprUnit(sdb::TmprUnit::TmprC);
            } else if (v == toInt(VehicleUnit::FAHRENHEIT)) {
                SIGNAL.setTmprUnit(sdb::TmprUnit::TmprF);
            } else {
                VHALOG.d("HalOnSetProperty HVAC_TEMPERATURE_DISPLAY_UNITS  Parameter not supported ");
                return;
            }
            setValue(toInt(VehicleArea::GLOBAL), v, true);
            if (!SIGNAL.usgModeIs(sdb::UsgModSts1::UsgModAbdnd)) {
                VHALOG.d("HalOnSetProperty HVAC_TEMPERATURE_DISPLAY_UNITS : setCB_VFC_SetVehMisc");
                SIGNAL.setCB_VFC_SetVehMisc(sdb::OnOff1::On);
            }
            IndcnUnit indcnUnit{};
            getCurrentIndcnUnit(indcnUnit, this);
            SIGNAL.setIndcnUnit(indcnUnit);

        });
}